package com.qzone;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{
	
	public SQLiteDatabase DB;
	public String DBPath;
	public static String DBName = "qzone";
	public static final int version = '1';
	public static Context currentContext;
	public static String tableName = "studentlist";
	

	public DBHelper(Context context) {
		super(context, DBName, null, version);
		currentContext = context;
		DBPath = "/data/data/" + context.getPackageName() + "/databases/";
		//DBPath = Environment.getExternalStorageDirectory()+"/" + context.getPackageName() + "/databases/";
		createDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

	private void createDatabase() {
		boolean dbExists = checkDbExists();
		
		if (dbExists) {
			// do nothingcreateDatabase
		} else {
			DB = currentContext.openOrCreateDatabase(DBName, 0, null);
			DB.execSQL("CREATE TABLE IF NOT EXISTS " +
        			tableName +
        			" (SIPNumber VARCHAR,MasterId VARCHAR,SchoolName VARCHAR, StudentName VARCHAR, ROLLNo VARCHAR,MobileNo VARCHAR,TagNo VARCHAR);");
        
		}
		
		
	}
	
	public long insertData(String SIPNumber,String MasterId,String SchoolName,String StudentName,String ROLLNo,String MobileNo,String TagNo)
	{
		DB = currentContext.openOrCreateDatabase(DBName, 0, null);
		ContentValues cv=new ContentValues();
		cv.put("SIPNumber", SIPNumber);
		cv.put("MasterId", MasterId);
		cv.put("SchoolName", SchoolName);
		cv.put("StudentName", StudentName);
		cv.put("ROLLNo", ROLLNo);
		cv.put("MobileNo", MobileNo);
		cv.put("TagNo", TagNo);
		return DB.insert(tableName, null, cv);
		
	}
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																										
	public long getNumRows(){
		DB = currentContext.openOrCreateDatabase(DBName, 0, null);
		return DatabaseUtils.queryNumEntries(DB, tableName);
	}
	
	public Cursor retriveAllRecord()
	{
		DB = currentContext.openOrCreateDatabase(DBName, 0, null);
		return DB.rawQuery("Select SIPNumber,MasterId,SchoolName,StudentName,ROLLNo,MobileNo,TagNo from "+tableName, null);
	}
	
	public void deleteRecord(String MasterId)
	{
		DB = currentContext.openOrCreateDatabase(DBName, 0, null);
		DB.execSQL("Delete from "+tableName+" where MasterId='"+MasterId+"';" );
	}
	
	private boolean checkDbExists() {
		SQLiteDatabase checkDB = null;

		try {
			String myPath = DBPath + DBName;
			checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
			//checkDB = SQLiteDatabase.openOrCreateDatabase(myPath, null);

		} catch (SQLiteException e) {

			// database does't exist yet.

		}

		if (checkDB != null) {

			checkDB.close();

		}

		return checkDB != null ? true : false;
	}
}